//
//  TopViewController.m
//  Movie
//
//  Created by mycomputer on 15/7/17.
//  Copyright (c) 2015年 杨韦智. All rights reserved.
//

#import "TopViewController.h"
#import "Base.h"
#import "DataService.h"
#import "TopModel.h"
#import "TopCollectionViewCell.h"

@interface TopViewController ()

@end

@implementation TopViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Top250";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initData];
    [self _loadData];
    [self _createCollectionView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_initData {
    _topModelArray = [[NSMutableArray alloc] init];
}

- (void)_loadData {
    
    NSDictionary *topDic = [DataService getJsonDataFromFile:@"top250.json"];
    NSArray *subjects = [topDic objectForKey:@"subjects"];
    for (NSDictionary *dic in subjects) {
        TopModel *model = [[TopModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_topModelArray addObject:model];
    }
    
}

- (void)_createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kWidth-40)/3, (kWidth-20)/3*1.5);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
  //  [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"topViewCell"];
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    UINib *nib = [UINib nibWithNibName:@"TopCollectionViewCell" bundle:nil];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"topViewCell"];
    [self.view addSubview:_collectionView];

}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _topModelArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topViewCell" forIndexPath:indexPath];
    cell.model = _topModelArray[indexPath.row];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 10, 10);
    return insets;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
